import request from '@/utils/request'

const UPLOAD_FILE = '/upload/file'
const UPLOAD_IMG = '/upload/image'
export default {
  file() {
    return UPLOAD_FILE
  },

  image() {
    return UPLOAD_IMG
  },

  fileUploadBase64(file) {
    return request({
      method: 'post',
      url: '/upload/imageBase64',
      data: {
        base64: file
      },
      transformRequest: (data) => {
        const fd = new FormData()
        for (const i in data) {
          fd.append(i, data[i])
        }
        return fd
      }
    })
  },

  fileUpload(file) {
    return request({
      method: 'post',
      url: UPLOAD_FILE,
      data: {
        file
      },
      transformRequest: (data) => {
        const fd = new FormData()
        for (const i in data) {
          fd.append(i, data[i])
        }
        return fd
      }
    })
  },

  imgUpload(file, progressFn) {
    return request({
      method: 'post',
      url: UPLOAD_IMG,
      data: {
        file
      },
      transformRequest: (data) => {
        const fd = new FormData()
        for (const i in data) {
          fd.append(i, data[i])
        }
        return fd
      },
      onUploadProgress: (progressEvent) => {
        if (progressFn) progressFn(progressEvent)
      }
    })
  }
}
